<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Requirement_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据用户ID返回商品备注信息
	 */
	public function find_by_user($userid){
		$this->db->where('userid', $userid);
		$query = $this->db->get('t_requirement');
		return $query->result_array();
	}
	/*
	 * 根据用户ID和商品ID返回商品备注信息
	 */
	public function find_by_user_and_product($userid,$productid){
		$this->db->where('userid', $userid);
		$this->db->where('productid', $productid);
		$query = $this->db->get('t_requirement');
		return $query->row_array();
	}
	/*
	 * 添加商品备注信息
	 */
	public function create($requirement) {
		$this->db->insert ( 't_requirement', $requirement );
		return $this->db->insert_id ();
	}
	/*
	 * 根据分页信息得到商品备注
	 */
	public function find_all_requirements($per_page,$offset){
		$this->db->select('t_requirement.id,t_user.shopname,t_product.name,t_requirement.content');
		$this->db->from('t_requirement');
		$this->db->join('t_user', 't_requirement.userid = t_user.id','left');
		$this->db->join('t_product', 't_requirement.productid = t_product.id','left');
		$this->db->where('userid!=17');
		$this->db->where('userid!=11');
		$this->db->where("userid!=48");
		$this->db->where("userid!=12");
		$this->db->where("userid!=22");
		$this->db->limit($per_page, $offset);
		$this->db->order_by("t_requirement.userid");
		$this->db->order_by("t_requirement.id", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 返回所有商品备注的数量
	 */
	public function count_requirements(){
		$query = $this->db->get('t_requirement');
		return $query->num_rows();
	}
	/*
	 *更新商品备注信息
	 */
	public function update($id,$requirement){
		$this->db->where('id', $id);
		$this->db->update('t_requirement', $requirement);
		return $this->db->affected_rows()>0;
	}
	/*
	 *删除商品备注
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_requirement');
		return $this->db->affected_rows()>0;
	}
}